package com.cjzuji.rental;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.cjzuji.rental.Bean.request.tool.CreateSmsRequest;
import com.cjzuji.rental.mapper.UserMapper;
import com.cjzuji.rental.pojo.UserData;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@SpringBootTest
public class HttpUrlConnectionExample {

    public static String sendCurlRequest(String url, String method, String body) {
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // 设置请求方法
            con.setRequestMethod(method);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("accesstoken", "AK2E262S97X66HFB41ACF8590JDBSBAMBFW3EHFDX54N5F83");
            con.setRequestProperty("authorization", "AK2E262S97X66HFB41ACF8590JDBSBAMBFW3EHFDX54N5F83");

            // 发送数据到服务器（例如，如果是POST请求）
            if (body != null && !body.isEmpty()) {
                con.setDoOutput(true);
                try (OutputStream os = con.getOutputStream()) {
                    os.write(body.getBytes());
                    os.flush();
                }
            }

            // 获取响应状态码
            int responseCode = con.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            // 读取响应内容
            try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                return response.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    @Autowired
//    private UserMapper userMapper;
//    @Test
//    public void testGetCount(){
//
//        CreateSmsRequest createSmsRequest = new CreateSmsRequest();
//        List<Map<String,String>> list = new ArrayList<>();
////        createSmsRequest.setFundAuthTypes(list);
//        String count = HttpUrlConnectionExample.sendCurlRequest("https://manage-api.qnvipmall.com/merchant/loan/v2/list",
//                "POST",
//                "{\"fundAuthTypes\":[],\"model\":\"transferAllList\",\"pageNo\":0,\"pageSize\":20,\"sendTypeFlag\":[],\"transferMerchantTag\":2}");
////        System.out.println("count " + count);
//
//        // 将string 转List<Map<String,String>>
//
//        //将list遍历  获取 map  根据map中的 id 获取订单详情
//        // count是返回的JSON字符串
//        if(!count.isEmpty()){
//            // 解析JSON字符串转为对象
//            JSONObject  jsonResponse = JSON.parseObject(count);
//            // 从jsonResponse对象中通过方法获取data对象（getJSONObject）
//            JSONObject  jsonData = jsonResponse.getJSONObject("data");
//            // 通过方法（getJSONArray）获取list数组
//            JSONArray jsonArray = jsonData.getJSONArray("list");
//            //获取code，success，message
//            String code = jsonResponse.getString("code");
//            String success = jsonResponse.getString("success");
//            String message = jsonResponse.getString("message");
//            if(success=="true"){
//                System.out.println("成功");
//                System.out.println("success:"+ success);
//                System.out.println("message:"+ message);
//                // 循环遍历list数组
//                for (int i = 0; i < jsonArray.size(); i++) {
//                    // 获取list数组中的每个元素但是是字符串
//                    String item = jsonArray.getString(i);
//                    // 这个时候要转为对象(JSON.parseObject)
//                    JSONObject jsonItem = JSON.parseObject(item);
//                    // 获取id通过方法（getString）
//                    String id = jsonItem.getString("id");//19218580
//                    System.out.println("id:"+ id);
//                    UserData userData = new UserData();
//                    userData.setReceiverName(jsonItem.getString("receiverName"));
//                    userMapper.save(userData);
//                }
//
//            }

 //       }
        //再将数据保存到数据库





 //   }
}